package com.mytools.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.zip.GZIPInputStream;

 
/**
 * 下载 工具�?
 * 
 * @author sun
 */
public class DownloadUtils {
     
    /**
     * 下载文件到本�?
     * @author sun
     * @date 2018�?3�?25�? 上午11:01:05
     * @param urlString
     * @param filename
     * @throws Exception
     */
	 public static void download(String urlString, String filename)
	            throws Exception {
	    	
	    	OutputStream os = null;
	    	GZIPInputStream gis = null;
	    	InputStream is = null;
	    	try {
	    		URL url = new URL(urlString);// 构�?�URL
	            URLConnection con = url.openConnection();// 打开连接
	             is = con.getInputStream();// 输入�?
	            String code = con.getHeaderField("Content-Encoding");
	            File file = new File(filename);
	            if (!file.getParentFile().exists()&&!file.isDirectory()){
	                file.getParentFile().mkdirs();
	            } 
	            if ((null != code) && code.equals("gzip")) {
	                gis = new GZIPInputStream(is);
	                // 1K的数据缓�?
	                byte[] bs = new byte[1024];
	                // 读取到的数据长度
	                int len;
	                // 输出的文件流
	                os = new FileOutputStream(filename);
	                // �?始读�?
	                while ((len = gis.read(bs)) != -1) {
	                    os.write(bs, 0, len);
	                }
	                // 完毕，关闭所有链�?
	                gis.close();
	                os.close();
	                is.close();
	            } else {
	                // 1K的数据缓�?
	                byte[] bs = new byte[1024];
	                // 读取到的数据长度
	                int len;
	                // 输出的文件流
	                os = new FileOutputStream(filename);
	                // �?始读�?
	                while ((len = is.read(bs)) != -1) {
	                    os.write(bs, 0, len);
	                }
	                // 完毕，关闭所有链�?
	                os.close();
	                is.close();
	            }
	    	}finally {
	    		if(os !=null) {
	    			os.close();
	    		}
	    		if(gis !=null) {
	    			gis.close();
	    		}
	    		if(is !=null) {
	    			is.close();
	    		}
	    	}
	        
	    }
     
    public static void main(String[] args) {
        try {
//            download("https://p0.meituan.net/myvideodistribute/74e66e8edef1e11537676b2978a2f7ad1011493.png",
//            		"E:\\File\\74e66e8edef1e11537676b2978a2f7ad1011493.png");
//            download("https://p1.meituan.net/myvideodistribute/2b0c7714a8e3b69b2bd97ad6c8af7cb0829367.png",
//            		"E:\\File\\2b0c7714a8e3b69b2bd97ad6c8af7cb0829367.png");
        	
//        	
//        	List<String> list = ImgMatchUtil.getMatchList(html,"//detail.tmall.com([^\"]*)",true);
//        	
//        	for (String str : list) {
//        		
//        		String url ="http:"+str.substring(0, str.indexOf("user_id="));
//        		System.out.println(url);
////        		String s=HttpsUtil.post(url, "", "utf-8");
////        		System.out.println(s);
//        		break;
//        	}
        	
        	DownloadUtils.download("https://img.alicdn.com/imgextra/i4/706084574/O1CN01jGxF4Q1jewDqmcjbr_!!706084574.jpg", "E:\\File\\74e66e8edef1e11537676b2978a2f7ad1011493.png");
        	
//        	List<String> list = ImgMatchUtil.listImgSrc(html);
//            for (String str : list) {
//            	try {
//            		download("http:"+str,
//                     		"E:\\File\\"+str.substring(str.lastIndexOf("/")+1));
//            		System.out.println(str.substring(str.lastIndexOf("/")+1));
//            		System.out.println(str);
//				} catch (Exception e) {
//					
//				}
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}
